﻿/** \file
* \$Rev: 184 $
* 
* \$Date: 2010-04-14 07:18:41 +0000 (Wed, 14 Apr 2010) $
*
* \$URL: http://chorus-twitter.googlecode.com/svn/trunk/src/gui/ViewForm.cs $
*/

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using chorus.utils;

namespace chorus.gui
{
	/// <summary>
	/// A view form containing a list of Twitter messages.
	/// </summary>
	public partial class ViewForm : Form
	{
		/// <summary>The view associated with this form.</summary>
		net.View view;
		/// <summary>The list of Twitter message rows.</summary>
		List<MessageRow> rows = new List<MessageRow>();
		/// <summary>Whether the window is closing. If this is true, don't issue a prompt.</summary>
		bool closing = false;

		/// <summary>The constructor.</summary>
		/// <param name="view">The view associated with this form.</param>
		public ViewForm(net.View view)
		{
			InitializeComponent();
			this.view = view;
			MdiParent = Program.mainform;
		}

		/// <summary>Close this window without issuing a prompt.</summary>
		public void CloseWithoutPrompt()
		{
			closing = true;
			Close();
		}

		/// <summary>Add a message to the view.</summary>
		/// <param name="msg">The message to add.</param>
		public void AddMessage(twitter.Message msg)
		{
			Utils.InvokeControl(this, delegate()
			{
				MessageRow row = new MessageRow(msg);
				rows.Add(row);
				tableMessages.Controls.Add(row);
			});
		}

		/// <summary>Refresh the message list.</summary>
		public void RefreshMessages() { view.FetchAsync(); }

		/// <summary>
		/// Fired when the view is closing. Since closing the view window kills
		/// the view itself, check with the user before letting them go through
		/// with it.
		/// </summary>
		/// <param name="sender">The event sender.</param>
		/// <param name="e">The event parameters.</param>
		void ViewForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (closing)
				return;
			if (e.CloseReason == CloseReason.UserClosing)
			{
				if (MessageBox.Show("Are you sure you want to remove this view?",
					"Warning", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					Program.views.Remove(view);
					closing = true;
				}
				else
					e.Cancel = true;
			}
			else closing = true;
		}
	}
}
